package czy.demo.service.security;

import czy.demo.pojo.system.Role;
import czy.demo.pojo.system.User;
import czy.demo.repository.PermissionRepository;
import czy.demo.repository.RoleRepository;
import czy.demo.repository.UserRepository;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/* 初始化服务，用于初始化系统用户、角色、权限 */
@Service
public class InitService implements InitializingBean {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private PermissionRepository permissionRepository;

    @Override
    public void afterPropertiesSet() throws Exception {

        Role role = new Role(1,"超级管理员","系统所有权限管理员",true,null);

        this.roleRepository.save(role);

        User user = new User(1,"admin",passwordEncoder.encode("123456"),"超级管理员",false,false,false,true, Arrays.asList(role));

        this.userRepository.save(user);

    }
}
